package com.hsy.crm.web.service.asyn;

import com.google.common.collect.Lists;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.entity.*;
import com.hsy.crm.web.mapper.SplitProductMapper;
import com.hsy.crm.web.mapper.StoneOtherMapper;
import com.hsy.crm.web.mapper.StoreProductDetailMapper;
import com.hsy.crm.web.service.asyn.inter.IOrderService;
import com.hsy.crm.web.service.impl.SplitProductService;
import com.hsy.crm.web.service.impl.TransferOrderService;
import com.hsy.crm.web.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AsynStoreService implements IOrderService {

    @Autowired
    StoreProductDetailMapper storeProductDetailMapper;

    @Autowired
    SplitProductMapper splitProductMapper;

    @Autowired
    StoneOtherMapper stoneOtherMapper;

    @Override
    public List<StoreProductDetail> addStock(Message message) {
        List<StoreProductDetail> result = null;
        switch (message.getTargetType()){
            //调拨
            case App.BILL_TYPE.ORDER_DB:
                result = add_stock_db(message);
                break;
            //拆装
            case App.BILL_TYPE.ORDER_CZ:
                result = add_stock_cz(message);
                break;
            //盘点
            case App.BILL_TYPE.ORDER_PD:
                result = add_stock_pd(message);
                break;
            //其它入库
            case App.BILL_TYPE.ORDER_QTRKD:
                result = add_stock_other(message);
                break;
            //其它出库
            case App.BILL_TYPE.ORDER_QTCKD:
                result = add_stock_other(message);
                break;
            default:
                break;
        }

        return result;
    }

    @Override
    public List<StoreProductDetail> getStoreProductDetail(Message message) {
        return null;
    }

    @Override
    public void relationAccount(Message message) {

    }

    private List<StoreProductDetail> add_stock_pd(Message message){
        List<StoreProductDetail> storeProductDetails = Lists.newArrayList();
        List<StoreCheckDetail> orderDetails = (List<StoreCheckDetail>)message.getOrderDetails();
        for( StoreCheckDetail orderDetail : orderDetails ){
            StoreProductDetail storeProductDetail = this.getBaseStoreProductDetail(message);
            storeProductDetail.setNumber(orderDetail.getChangeNumber());
            storeProductDetail.setPrice(orderDetail.getCheckPrice());
            storeProductDetail.setStoreProductId(orderDetail.getStoreProductId());
            storeProductDetail.setType(getPdDirection(orderDetail));
            storeProductDetail.setOrderType(App.BILL_TYPE.ORDER_QTRKD);
            storeProductDetail.setOrderDetailId(orderDetail.getId());
            storeProductDetail.setOrderId(orderDetail.getStoreCheckId());
            storeProductDetails.add(storeProductDetail);
            storeProductDetailMapper.insert(storeProductDetail);
        }
        return storeProductDetails;
    }

    private int getPdDirection(StoreCheckDetail storeCheckDetail){
        if( storeCheckDetail.getChangeNumber() > 0 ){
            return App.StoreType.IN;
        }else{
            return App.StoreType.OUT;
        }
    }

    private List<StoreProductDetail> add_stock_other(Message message){
        StoneOther stoneOther = new StoneOther();
        stoneOther.setCustomerId(message.getCustomerId());
        stoneOther.setId(message.getTargetId());
        stoneOther = stoneOtherMapper.selectByPrimaryKey(stoneOther);

        List<StoreProductDetail> storeProductDetails = Lists.newArrayList();
        List<StoneOtherDetail> orderDetails = (List<StoneOtherDetail>)message.getOrderDetails();
        for( StoneOtherDetail orderDetail : orderDetails ){
            StoreProductDetail storeProductDetail = this.getBaseStoreProductDetail(message);
            storeProductDetail.setNumber(orderDetail.getNumber());
            storeProductDetail.setPrice(orderDetail.getPrice());
            storeProductDetail.setStoreProductId(orderDetail.getStoreProductId());
            storeProductDetail.setType(stoneOther.getOtherType());
            storeProductDetail.setOrderType(message.getTargetType());
            storeProductDetail.setOrderDetailId(orderDetail.getId());
            storeProductDetail.setOrderId(orderDetail.getStoneOtherId());
            storeProductDetails.add(storeProductDetail);
            storeProductDetailMapper.insert(storeProductDetail);
        }
        return storeProductDetails;
    }

    private List<StoreProductDetail> add_stock_cz(Message message){
        SplitProduct splitProduct = new SplitProduct();
        splitProduct.setCustomerId(message.getCustomerId());
        splitProduct.setId(message.getTargetId());
        splitProduct = splitProductMapper.selectByPrimaryKey(splitProduct);

        List<StoreProductDetail> storeProductDetails = Lists.newArrayList();
        List<SplitProductDetail> orderDetails = (List<SplitProductDetail>)message.getOrderDetails();
        for( SplitProductDetail orderDetail : orderDetails ){
            StoreProductDetail storeProductDetail = this.getBaseStoreProductDetail(message);
            storeProductDetail.setNumber(orderDetail.getNumber());
            storeProductDetail.setPrice(SplitProductService.getPrice(orderDetail));
            storeProductDetail.setStoreProductId(orderDetail.getStoreProductId());
            storeProductDetail.setType(SplitProductService.getDirection(splitProduct.getType(),orderDetail));
            storeProductDetail.setOrderType(App.BILL_TYPE.ORDER_CZ);
            storeProductDetail.setOrderDetailId(orderDetail.getId());
            storeProductDetail.setOrderId(orderDetail.getSplitProductId());
            storeProductDetails.add(storeProductDetail);
            storeProductDetailMapper.insert(storeProductDetail);
        }
        return storeProductDetails;
    }

    private List<StoreProductDetail> add_stock_db(Message message){
        List<StoreProductDetail> storeProductDetails = Lists.newArrayList();
        List<TransferOrderDetail> orderDetails = (List<TransferOrderDetail>)message.getOrderDetails();
        for( TransferOrderDetail orderDetail : orderDetails ){
            StoreProductDetail storeProductDetail = this.getBaseStoreProductDetail(message);
            storeProductDetail.setOrderId(orderDetail.getTransferOrderId());

            storeProductDetail.setNumber(orderDetail.getNumber());
            storeProductDetail.setPrice(TransferOrderService.getPrice(orderDetail,orderDetail.getOutPrice()));
            storeProductDetail.setStoreProductId(orderDetail.getOutStoreProductId());
            storeProductDetail.setType(App.StoreType.OUT);
            storeProductDetail.setOrderType(App.BILL_TYPE.ORDER_DB);
            storeProductDetail.setOrderDetailId(orderDetail.getId());
            storeProductDetails.add(storeProductDetail);
            storeProductDetailMapper.insert(storeProductDetail);

            storeProductDetail = this.getBaseStoreProductDetail(message);
            storeProductDetail.setNumber(orderDetail.getNumber());
            storeProductDetail.setPrice(TransferOrderService.getPrice(orderDetail,orderDetail.getInPrice()));
            storeProductDetail.setStoreProductId(orderDetail.getInStoreProductId());
            storeProductDetail.setType(App.StoreType.IN);
            storeProductDetail.setOrderType(App.BILL_TYPE.ORDER_DB);
            storeProductDetail.setOrderDetailId(orderDetail.getId());
            storeProductDetails.add(storeProductDetail);
            storeProductDetailMapper.insert(storeProductDetail);
        }

        return storeProductDetails;
    }

    private StoreProductDetail getBaseStoreProductDetail(Message message){
        StoreProductDetail storeProductDetail = new StoreProductDetail();
        storeProductDetail.setId(UuidUtils.getUuid());
        storeProductDetail.setBackNumber(0L);
        //storeProductDetail.setNumber(orderDetail.getNumber());
        //storeProductDetail.setPrice(this.getPrice(orderDetail));
        storeProductDetail.setSort(0L);
        //storeProductDetail.setStoreProductId(orderDetail.getStoreProductId());
        //storeProductDetail.setType(type);
        //storeProductDetail.setOrderType(orderDetail.getOrderType());
        //storeProductDetail.setOrderDetailId(orderDetail.getId());
        storeProductDetail.setRelationStartSort(-1L);
        storeProductDetail.setRelationEndSort(-1L);
        storeProductDetail.setCustomerId(message.getCustomerId());
        storeProductDetail.setTime(message.getTime());
        storeProductDetail.setOutPrice(0L);
        storeProductDetail.setOutNumber(0L);
        storeProductDetail.setTmpPrice(0L);
        storeProductDetail.setRushBack(App.Booleans.NO);
        return storeProductDetail;
    }

}
